<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑业主</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <!-- 使用CDN引入Layui CSS -->
    <link href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css" media="all" rel="stylesheet">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" lay-filter="formDemo">
            <input name="userId" type="hidden">

            <div class="layui-form-item">
                <label class="layui-form-label">房产信息</label>
                <div class="layui-input-block">
                    <input class="layui-input" disabled name="houseInfo" type="text">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">业主姓名</label>
                <div class="layui-input-block">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="name"
                           placeholder="请输入业主姓名"
                           required type="text">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-block">
                    <input autocomplete="off" class="layui-input" lay-verify="required|phone" name="phone"
                           placeholder="请输入联系电话"
                           required type="text">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="formDemo" lay-submit>立即提交</button>
                    <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="lib/layui/layui.js"></script>
<script src="layui/layui.js"></script>
<script src="js/x-layui.js"></script>

<script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
<script>
    layui.use(['form', 'jquery', 'layer'], function () {
        var form = layui.form;
        var $ = layui.$; // 注意这里改为layui.$
        var layer = layui.layer;

        // 获取URL参数
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }

        // 加载业主信息
        var userId = getQueryVariable('id');
        console.log(userId)
        $.ajax({
            url: '/manager/owner/id',
            type: 'get',
            data: {id: userId},
            success: function (res) {
                if (res.code == 200) {
                    var data = res.data;
                    form.val('formDemo', {
                        "userId": data.userId,
                        "houseInfo": data.houseName,
                        "name": data.name,
                        "phone": data.phone
                    });
                }
            }
        });

        // 监听提交
        form.on('submit(formDemo)', function (data) {
            console.log(data.field)
            $.ajax({
                url: 'manager/updateOwner',
                type: 'post',
                data: {
                    ownerId: data.field.userId,
                    phone: data.field.phone,
                    photoUrl: null
                },
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        setTimeout(function () {
                            // 关闭当前iframe
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            // 刷新父页面
                            parent.location.reload();
                        }, 1000);
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
